#include <iostream>
#include <signal.h>     // sigemptyset(), sigaction()

using namespace std;


void Print(const sigset_t& pending)
{
    cout << "current process pending: ";

    for(int sig = 31; sig >= 1; --sig)
    {
        if(sigismember(&pending, sig) == 1) { cout << "1"; }
        else{
            cout << "0";
        }
    }
    cout << endl;
}

void handler(int signo)
{
    cout << "signal: " << signo << endl;
    
    sigset_t pending;
    sigemptyset(&pending);
    while(true)
    {
        sigpending(&pending);   // get pending map from system call
        Print(pending);
        sleep(1);
    }
}

void test()
{
    struct sigaction act, o_act;
    act.sa_handler = handler;
    sigemptyset(&(act.sa_mask));    // clear a variable which type is "sigset_t"
    act.sa_flags = 0;

    sigaction(2, &act, &o_act);     // run the handler which has set in "sa_handler" when signal No.2 is called

    cout << "catch singal test process pid: " << getpid() << endl;
    for(int i = 0; i < 10; i++)
    {
        cout << "Waiting for blocking signal of No.2" << endl;
        sleep(1);
    }
}


int main()
{
    test();

    return 0;
}
